package com.foreknow.controller;

import com.foreknow.bean.Users;
import com.foreknow.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**\
 * Servlet服务器端的应用程序
 * 1.需要继承HttpServlet
 * 2.重写doGET/doPost方法
 *  这两个方法会根据前端的请求来调用,如果前端发送的请求是get请求(例如<a>链接</a>),就会自动调用
 *      ServLet中的doget方法
 *      如果前端发送的请求是post请求(例如<form method="post"> 表单<form/>)就会自动调用
 *      ServLet中的dopost方法
 * 3. HttpServletRequest请求对象
 * 4.HttpServletResponse响应对象
 */
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //服务器端向客户端响应的内容的类型
//        //编码格式是utf-8
//        resp.setContentType("text/html;charset=utf-8");
//        //字符流   获取PrintWriter打印流  作用是向网页写一些基本的信息
//        PrintWriter out = resp.getWriter();
//        out.println("<html>");
//        out.println("<head>");
//        out.println("<title>serclet</title>");
//        out.println("</head>");
//        out.println("<body>");
//        out.println(new Date());
//        out.println("</body>");
//        out.println("</html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        //向网页响应内容  字符流
        PrintWriter out = resp.getWriter();
        String username = req.getParameter("username");
        String password = req.getParameter("password");
//        out.println(username+"-------"+password);
        UserServiceImpl userService = new UserServiceImpl();
        Users users = userService.query(username,password);
        if (users!=null){
//            out.println("<a href=''>查看所有的留言的信息</a>");
//            out.println(users.getAdminname());
            //请求转发
            req.getRequestDispatcher("/m?q=select").forward(req,resp);
            //重定向
//            resp.sendRedirect(req.getContextPath()+"/get");
//            resp.sendRedirect("http://www.163.com");
        }else {
            out.println("error.....");
        }
    }
}
